<?php

/**
 * @author Laurent SITTLER <lolo.sittler@gmail.com>
 * @author Alexandre GOMEZ <gomez.alexandre02@gmail.com>
 * @version 0.1
 */
require_once 'User_Model.php';
require_once 'Database_Model.php';
require_once 'Requete_Model.php';

/**
 * Index Model Class
 */
class Index_Model extends Model {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
    }

    /**
     * Create a new user
     */
    function nuser() {
        if (isset($_POST['firstName']) && !empty($_POST['firstName'])
                && isset($_POST['LastName']) && !empty($_POST['LastName'])
                && isset($_POST['login']) && !empty($_POST['login'])
                && isset($_POST['password']) && !empty($_POST['password'])
                && isset($_POST['vpassword']) && !empty($_POST['vpassword'])) {
            if ($_POST['password'] == $_POST['vpassword']) {
                $chkAdmin = 0;
                $chkStatus = 1;
                if (isset($_POST['status']))
                    $chkStatus = 0;
                if (isset($_POST['admin']))
                    $chkAdmin = 1;

                $usr = new User_Model();
                $lastInsert = $usr->InsertUser($_POST['login'], $_POST['password'], $_POST['firstName'], $_POST['LastName'], $chkStatus, $chkAdmin);
                $chk = new User_Model();
                $chk->User_Log(Session::Get(Constants::$SESSION_cuser), $_SERVER["REMOTE_ADDR"], 'Ajout de l\'utilisateur id : ' . $lastInsert . '');
            } else {
                echo 'Pas le bon mon de passe';
            }
        } else {
            echo 'Des champs sont incomplets';
        }
        Session::Set(Constants::$SESSION_currentTabs, $_POST['current-tab']);
        header('location: ' . $GLOBALS['conf']['interface']['url']);
        exit;
    }

    /**
     * Delete user(s)
     */
    function duser() {
        $usr = new User_Model();
        foreach (array_keys($_POST) as $key) {
            if (strpos($key, 'chk-') !== false) {
                $usr->DeleteUser(str_replace('chk-', '', $key));
                $chk = new User_Model();
                $chk->User_Log(Session::Get(Constants::$SESSION_cuser), $_SERVER["REMOTE_ADDR"], 'Suppression  de l\'utilisateur id : ' . str_replace('chk-', '', $key) . '');
            }
        }
        Session::Set(Constants::$SESSION_currentTabs, $_POST['current-tab']);
        header('location: ' . $GLOBALS['conf']['interface']['url']);
        exit;
    }

    /**
     * Delete Database(s)
     */
    function ddatabase() {
        $db = new Database_Model();
        foreach (array_keys($_POST) as $key) {
            if (strpos($key, 'chk-') !== false) {
                $db->DeleteDB(str_replace('chk-', '', $key));
                $chk = new User_Model();
                $chk->User_Log(Session::Get(Constants::$SESSION_cuser), $_SERVER["REMOTE_ADDR"], 'Suppression de la Base de donn&eacute;es id : ' . str_replace('chk-', '', $key) . '');
            }
        }
        Session::Set(Constants::$SESSION_currentTabs, $_POST['current-tab']);
        header('location: ' . $GLOBALS['conf']['interface']['url']);
        exit;
    }

    /**
     * Add a database
     */
    function ndatabase() {
        if (isset($_POST['nickname']) && !empty($_POST['nickname'])
                && isset($_POST['host']) && !empty($_POST['host'])
                && isset($_POST['port']) && !empty($_POST['port'])
                && isset($_POST['dbname']) && !empty($_POST['dbname'])
                && isset($_POST['dbtype']) && !empty($_POST['dbtype'])
                && isset($_POST['dbrule']) && !empty($_POST['dbrule'])) {
            $db = new Database_Model();
            $insert = $db->AddDB($_POST['nickname'], $_POST['dbtype'], $_POST['host'], $_POST['port'], $_POST['dbname'], Session::Get(Constants::$SESSION_cuser), $_POST['dbrule']);
            $chk = new User_Model();
            $chk->User_Log(Session::Get(Constants::$SESSION_cuser), $_SERVER["REMOTE_ADDR"], 'Ajout de la Base de donn&eacute;es id : ' . $insert . '');
        } else {
            echo 'Des champs sont incomplets';
        }
        Session::Set(Constants::$SESSION_currentTabs, $_POST['current-tab']);
        header('location: ' . $GLOBALS['conf']['interface']['url']);
        exit;
    }

    /**
     * Run user request
     */
    function nrequete() {
        if (isset($_POST['tRequest']) && !empty($_POST['tRequest'])) {
            $sql = $_POST['tRequest'];

            Session::Set(Constants::$SESSION_valid, $sql);
            Session::Set(Constants::$SESSION_sql, $sql);

            $db = new Requete_Model();
            $result = $db->SetRequest($sql);
            Session::Set(Constants::$SESSION_resultRequest, $result);
        }
        Session::Set(Constants::$SESSION_currentTabs, $_POST['current-tab']);
        header('location: ' . $GLOBALS['conf']['interface']['url']);
        exit;
    }

    /**
     * Update user password
     */
    function userPwd() {
        $updUser = new User_Model();
        $updUser->UpdatePassword(Session::Get(Constants::$SESSION_cuser), $_POST['first']);
    }

    /**
     * Select a database
     */
    function SelectDB() {
        Session::Set(Constants::$SESSION_cdb, $_POST['dbselect']);
        Session::Set(Constants::$SESSION_ldb, $_POST['userDB']);
        Session::Set(Constants::$SESSION_pdb, $_POST['pwdDB']);
    }

}